﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Public;
using DLL;

namespace QLBanVe
{
    public partial class frmNhanLichChuyenBay : UserControl
    {
        public frmNhanLichChuyenBay()
        {
            InitializeComponent();
        }
        SanBayDLL sbdll = new SanBayDLL();
        SanBayPublic sbp = new SanBayPublic();
        ChuyenBayDLL cbdll = new ChuyenBayDLL();
        ChuyenBayPublic cbp = new ChuyenBayPublic();

        string SanBayDen = "";

        //Hàm load san bay vao combox
        private void Loadcb()
        {
            //load combox sân bay đi
            cbSanBayDi.DataSource = sbdll.LoadSanBay();
            cbSanBayDi.DisplayMember = "TenSanBay";
            cbSanBayDi.ValueMember = "MaSanBay";
            cbSanBayDi.SelectedIndex = -1;
        }
      
        public void XLTextbox(Boolean t)
        {
            txtMaChuyenBay.ReadOnly = t;
            txtSLGhe1.ReadOnly = t;
            txtSLGhe2.ReadOnly = t;
            txtThoiGianBay.ReadOnly = t;
            cbSanBayDi.Enabled = !t;
            cbSanBayDen.Enabled = !t;
            dNgayGio.Enabled = !t;
            txtGiaTien1.ReadOnly = t;
            txtMaChuyenBay.ReadOnly = !t;
        }


        private void frmNhanLichChuyenBay_Load(object sender, EventArgs e)
        {

            btnLuu.Enabled = true;
            btnSua.Enabled = false;
            btnCapNhat.Enabled = false;
            btnXoa.Enabled = false;
            btnHuy.Enabled = false;
            

            txtMaChuyenBay.Focus();
            txtMaChuyenBay.Text = "";
            txtThoiGianBay.Text = "";
            txtSLGhe1.Text = "";
            txtSLGhe2.Text = "";
            txtGiaTien1.Text = "";

            XLTextbox(false);
            txtMaChuyenBay.Enabled = true;
            Loadcb();

            dgwChuyenBay.DataSource = cbdll.LoadChuyenBay();
            DataTable dt = cbdll.LoadChuyenBay();
            int i = dt.Rows.Count+1;
            if (i < 10)
            {
                txtMaChuyenBay.Text = "CB0" + i;
                
            }
            else if (i > 9)
                txtMaChuyenBay.Text = "CB" + i;

        }

        private void dgwChuyenBay_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            int dong;
            dong = e.RowIndex;
            this.txtMaChuyenBay.Text = dgwChuyenBay.Rows[dong].Cells[1].Value.ToString();
            this.dNgayGio.Text = dgwChuyenBay.Rows[dong].Cells[2].Value.ToString(); ;
            this.txtThoiGianBay.Text = dgwChuyenBay.Rows[dong].Cells[3].Value.ToString();
            this.txtSLGhe1.Text = dgwChuyenBay.Rows[dong].Cells[4].Value.ToString();
            this.txtSLGhe2.Text = dgwChuyenBay.Rows[dong].Cells[5].Value.ToString();
            this.txtGiaTien1.Text = dgwChuyenBay.Rows[dong].Cells[6].Value.ToString();
            this.cbSanBayDi.Text=dgwChuyenBay.Rows[dong].Cells[7].Value.ToString();
            this.cbSanBayDen.Text = dgwChuyenBay.Rows[dong].Cells[8].Value.ToString();
            XLTextbox(true);
            txtMaChuyenBay.Enabled = false;

            btnLuu.Enabled = false;
            btnSua.Enabled = true;
            btnCapNhat.Enabled = false;
            btnXoa.Enabled = true;
            btnHuy.Enabled = true;
        }

        private void dgwChuyenBay_RowPrePaint(object sender, DataGridViewRowPrePaintEventArgs e)
        {
            for (int i = 0; i < dgwChuyenBay.RowCount; i++)
            {
                dgwChuyenBay.Rows[i].Cells[0].Value = i + 1;
            }
        }

       
        private void btnLuu_Click(object sender, EventArgs e)
        {
            try
            {
                if (txtMaChuyenBay.Text == "" || txtThoiGianBay.Text == "" || txtSLGhe1.Text == "" || txtSLGhe2.Text == "")
                {
                    MessageBox.Show("Bạn cần nhập đủ thông tin");
                }
                else
                {
                    TuyenBayPublic tbp = new TuyenBayPublic();
                    TuyenBayDLL tbdll = new TuyenBayDLL();
                    if (cbSanBayDi.SelectedIndex != -1)//Lấy giá trị từ combox san bay đi
                    {
                        int i = cbSanBayDi.SelectedIndex;
                        DataTable dt = new DataTable();
                        dt = sbdll.LoadSanBay();
                        DataRow dr;
                        dr = dt.Rows[i];

                        tbp.SanBayDi = dr["MaSanBay"].ToString();
                    }
                    if (cbSanBayDen.SelectedIndex != -1)//Lấy giá trị từ combox san bay đến
                    {
                        int i = cbSanBayDen.SelectedIndex;
                        DataTable dt = new DataTable();
                        dt = sbdll.LoadSanBayTheoTuyenBay(sbp);
                        DataRow dr;
                        dr = dt.Rows[i];
                        tbp.SanBayDen = dr["SanBayDen"].ToString();
                        SanBayDen = dr["SanBayDen"].ToString();
                        

                    }
                    DataTable bang = new DataTable();
                    bang = tbdll.LayMaTuyenBay(tbp);
                    DataRow dong;
                    dong = bang.Rows[0];
                    string MaTuyenBay = Convert.ToString(dong["MaTuyenBay"].ToString());//Lấy mã tuyến bay

                    if (cbSanBayDi.SelectedIndex != -1)//Lấy giá trị từ combox san bay đi
                    {
                        int i = cbSanBayDi.SelectedIndex;
                        DataTable dt = new DataTable();
                        dt = sbdll.LoadSanBay();
                        DataRow dr;
                        dr = dt.Rows[i];
                        cbp.SanBayDi = dr["TenSanBay"].ToString();
                    }

                    cbp.SanBayDen = SanBayDen;

                    cbp.MaChuyenBay = txtMaChuyenBay.Text.Trim();
                    cbp.MaTuyenBay = MaTuyenBay;
                    cbp.NgayGio = dNgayGio.Value;
                    cbp.ThoiGianBay = int.Parse(txtThoiGianBay.Text.Trim());
                    cbp.SoLuongGheHang1 = int.Parse(txtSLGhe1.Text.Trim());
                    cbp.SoLuongGheHang2 = int.Parse(txtSLGhe2.Text.Trim());
                    cbp.GiaTien = txtGiaTien1.Text;

                    //Thêm trình trang vé
                    TrinhTrangVePublic ttp = new TrinhTrangVePublic();
                    TrinhTrangVeDLL tt = new TrinhTrangVeDLL();
                    int SLGheTrong = int.Parse(txtSLGhe1.Text) + int.Parse(txtSLGhe2.Text);
                    ttp.SoLuongGheTrong = SLGheTrong;
                    ttp.SoLuongGheDat = 0;
                    ttp.MaChuyenBay = txtMaChuyenBay.Text.Trim();
                    tt.ThemTrinhTrangVe(ttp);
                    ///
                    //Thêm vào đơn giá
                    DonGiaPublic dgp = new DonGiaPublic();
                    DonGiaDLL dgdll = new DonGiaDLL();
                    dgp.MaChuyenBay = txtMaChuyenBay.Text.Trim();
                    dgp.DonGia = txtGiaTien1.Text;
                    dgdll.DonGia_Them(dgp);
                    ///
                    int ai = cbdll.ThemChuyenBay(cbp);
                    if (ai > 0)
                    {
                        cbSanBayDen.Text = "";
                        frmNhanLichChuyenBay_Load(sender, e);

                    }
                }
            }
            catch
            {
                MessageBox.Show("Mã chuyến bay đã tồn tại hoặc dữ liệu nhập không hợp lệ");
            }

            }   
  

        private void btnThem_Click(object sender, EventArgs e)
        {


            btnLuu.Enabled = true;
            btnSua.Enabled = false;
            btnCapNhat.Enabled = false;
            btnXoa.Enabled = false;
            btnHuy.Enabled = true;

            txtMaChuyenBay.Enabled = true;
            txtMaChuyenBay.Text = "";
            txtThoiGianBay.Text = "";
            txtSLGhe1.Text = "";
            txtSLGhe2.Text = "";
            cbSanBayDi.Text = "";
            cbSanBayDen.Text = "";
            txtMaChuyenBay.Focus();
            XLTextbox(false);
        }

        private void btnHuy_Click(object sender, EventArgs e)
        {
            frmNhanLichChuyenBay_Load(sender,e);
            txtMaChuyenBay.Focus();
            cbSanBayDi.Text = "";
            cbSanBayDen.Text = "";

        }
        private void cbSanBayDi_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cbSanBayDi.SelectedIndex != -1)//Lấy giá trị từ combox san bay đi
            {
                
                sbp.MaSanBay = cbSanBayDi.SelectedValue.ToString();//Lấy mã sân bay

              
                //load combox sân bay đến
                cbSanBayDen.DataSource = sbdll.LoadSanBayTheoTuyenBay(sbp);

                cbSanBayDen.DisplayMember = "SanBayDen";
                cbSanBayDen.ValueMember = "SanBayDen";
                
             

                cbSanBayDen.SelectedIndex = -1;
                
            }
        }

        private void btnThemSanBay_Click(object sender, EventArgs e)
        {

            frmAddSanBay fr = new frmAddSanBay();
            fr.Show();
        }

       
        private void btnCapNhat_Click(object sender, EventArgs e)
        {


            try
            {
                if (txtMaChuyenBay.Text == "" || txtThoiGianBay.Text == "" || txtSLGhe1.Text == "" || txtSLGhe2.Text == "")
                {
                    MessageBox.Show("Bạn cần nhập đủ thông tin");
                }
                else
                {
                    string SanBayDen1 = "";
                    TuyenBayPublic tbp = new TuyenBayPublic();
                    TuyenBayDLL tbdll = new TuyenBayDLL();
                    if (cbSanBayDi.SelectedIndex != -1)//Lấy giá trị từ combox san bay đi
                    {
                        int i = cbSanBayDi.SelectedIndex;
                        DataTable dt = new DataTable();
                        dt = sbdll.LoadSanBay();
                        DataRow dr;
                        dr = dt.Rows[i];

                        tbp.SanBayDi = dr["MaSanBay"].ToString();
                    }
                    if (cbSanBayDen.SelectedIndex != -1)//Lấy giá trị từ combox san bay đến
                    {
                        int i = cbSanBayDen.SelectedIndex;
                        DataTable dt = new DataTable();
                        dt = sbdll.LoadSanBayTheoTuyenBay(sbp);
                        DataRow dr;
                        dr = dt.Rows[i];
                        tbp.SanBayDen = dr["SanBayDen"].ToString();
                        SanBayDen1 = dr["SanBayDen"].ToString();
                    }
                    //Thêm mã tuyến bay
                    DataTable bang = new DataTable();
                    bang = tbdll.LayMaTuyenBay(tbp);
                    DataRow dong;
                    dong = bang.Rows[0];
                    string MaTuyenBay = Convert.ToString(dong["MaTuyenBay"].ToString());//Lấy mã tuyến bay
                    cbp.MaChuyenBay = txtMaChuyenBay.Text.Trim();

                    //Lấy sân bay đi từ combobox
                    if (cbSanBayDi.SelectedIndex != -1)//Lấy giá trị từ combox san bay đi
                    {
                        int i = cbSanBayDi.SelectedIndex;
                        DataTable dt = new DataTable();
                        dt = sbdll.LoadSanBay();
                        DataRow dr;
                        dr = dt.Rows[i];
                        cbp.SanBayDi = dr["TenSanBay"].ToString();

                    }

                    string Tien = txtGiaTien1.Text;
                    cbp.MaTuyenBay = MaTuyenBay;
                    cbp.SanBayDen = SanBayDen1;
                    cbp.NgayGio = dNgayGio.Value;
                    cbp.ThoiGianBay = int.Parse(txtThoiGianBay.Text.Trim());
                    cbp.SoLuongGheHang1 = int.Parse(txtSLGhe1.Text.Trim());
                    cbp.SoLuongGheHang2 = int.Parse(txtSLGhe2.Text.Trim());
                    cbp.GiaTien = Tien;

                    //Cập nhật đơn giá
                    DonGiaPublic dgp = new DonGiaPublic();
                    DonGiaDLL dgdll = new DonGiaDLL();
                    dgp.MaChuyenBay = txtMaChuyenBay.Text;
                    dgp.DonGia = Tien;
                    dgdll.DonGia_Edit(dgp);

                    //Cập nhật trình trang vé

                    TrinhTrangVePublic ttp = new TrinhTrangVePublic();
                    TrinhTrangVeDLL tt = new TrinhTrangVeDLL();
                    int SLGheTrong = int.Parse(txtSLGhe1.Text) + int.Parse(txtSLGhe2.Text);
                    ttp.SoLuongGheTrong = SLGheTrong;
                    ttp.SoLuongGheDat = 0;
                    ttp.MaChuyenBay = txtMaChuyenBay.Text.Trim();
                    tt.TrinhTrangVe_update(ttp);
                    ///
                    int ai = cbdll.SuaChuyenBay(cbp);
                    if (ai > 0)
                    {
                        frmNhanLichChuyenBay_Load(sender, e);

                    }
                }
            }
            catch
            {
                MessageBox.Show("Mã chuyến bay đã tồn tại hoặc dữ liệu nhập không hợp lệ");
            }
          
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            cbp.MaChuyenBay = txtMaChuyenBay.Text.Trim();

            DialogResult rs = new DialogResult();
            rs = MessageBox.Show("Bạn có chắt xóa không", "Thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (rs == DialogResult.Yes)
            {
                cbdll.XoaChuyenBay(cbp);
                //Xóa trình trạng vé
                TrinhTrangVePublic ttvp = new TrinhTrangVePublic();
                TrinhTrangVeDLL ttvdll = new TrinhTrangVeDLL();
                ttvp.MaChuyenBay = txtMaChuyenBay.Text.Trim();
                ttvdll.TrinhTrangVe_Xoa(ttvp);
                ///Xóa đơn giá
                DonGiaPublic dgp = new DonGiaPublic();
                DonGiaDLL dgdll = new DonGiaDLL();
                dgp.MaChuyenBay = txtMaChuyenBay.Text;
                dgdll.DonGia_Xoa(dgp);
                frmNhanLichChuyenBay_Load(sender, e);

            }
        }

        private void btnSua_Click(object sender, EventArgs e)
        {
             XLTextbox(false);
             
             btnLuu.Enabled = false;
             btnSua.Enabled = false;
             btnCapNhat.Enabled = true;
             btnXoa.Enabled = true;
             btnHuy.Enabled = true;
        }

     

        private void btnSanBayDi_Click(object sender, EventArgs e)
        {
            frmAddSanBay frm = new frmAddSanBay();
            frm.Show();
        }

        private void btnSanBayDen_Click(object sender, EventArgs e)
        {
            frmThemTuyenBay frm = new frmThemTuyenBay();
            frm.Show();
        }
        private void cbSanBayDen_SelectedIndexChanged(object sender, EventArgs e)
        {
            if(cbSanBayDen.SelectedIndex !=-1)
            SanBayDen = cbSanBayDen.SelectedValue.ToString();//Lấy giá trị sân bay đến từ combobox
          

      
        }
      

        private void txtMaChuyenBay_TextChanged(object sender, EventArgs e)
        {

        }

        private void dgwChuyenBay_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }
    }
}
